package com.niit.session19;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo {
	
	private static String url = "jdbc:mysql://localhost:3306/student_2021";
	private static String username = "root";
	private static String password = "root";
	
	
	public static void main(String[] args) {
//		1.下载驱动程序，把mysql-connector-java-8.0.27.jar
//		放到项目的lib文件夹，右键mysql-connector-java-8.0.27.jar选择build path ->add to build path
		
		Connection connection =null;
		Statement statement = null;
		ResultSet rs = null;
			try {
//		2.加载驱动程序：
//			全限定名称：包名加类名
//			com.mysql.cj.jdbc.Driver
				Class.forName("com.mysql.cj.jdbc.Driver");
//			3.获取数据库链接
				connection = DriverManager.getConnection(url,username,password);
//			4.执行SQL语句
				statement = connection.createStatement();
				rs = statement.executeQuery("select * from student where batch =2");
//			访问结果集
				while (rs.next()) {
					System.out.println(rs.getString("sid")+"---"+rs.getString("name"));
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally {
				try {
					connection.close();
					statement.close();
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
			}
			
		
	}

}
